package com.ruoyi.safe.controller.web;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.safe.domain.CommonCooperatorClass;
import com.ruoyi.safe.domain.vo.CommonCooperatorClassVO;
import com.ruoyi.safe.service.ICommonCooperatorClassService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 企业班组----->zyx
 *
 * @author buxi
 * @date 2023-12-20
 */

@RestController
@RequestMapping("/cooperator/class")
@RequiredArgsConstructor
public class CommonCooperatorClassController extends BaseController {

    private final ICommonCooperatorClassService commonCooperatorClassService;

    /**
     * 查询企业班组列表
     */
    @PreAuthorize("@ss.hasPermi('cooperator:class:list')")
    @GetMapping("/list")
    public TableDataInfo list(CommonCooperatorClass commonCooperatorClass) {
        if (ObjectUtil.isNull(commonCooperatorClass.getProjectId())) {
            return getDataTable(new ArrayList<>());
        }
        startPage();
        List<CommonCooperatorClassVO> list = commonCooperatorClassService.selectCommonCooperatorClassList(commonCooperatorClass);
        return getDataTable(list);
    }

    /**
     * 获取企业班组详细信息
     */
    @PreAuthorize("@ss.hasPermi('cooperator:class:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(commonCooperatorClassService.selectCommonCooperatorClassById(id));
    }

    /**
     * 新增企业班组
     */
    @PreAuthorize("@ss.hasPermi('cooperator:class:add')")
    @Log(title = "企业班组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CommonCooperatorClass commonCooperatorClass) {
        return toAjax(commonCooperatorClassService.insertCommonCooperatorClass(commonCooperatorClass));
    }

    /**
     * 修改企业班组
     */
    @PreAuthorize("@ss.hasPermi('cooperator:class:edit')")
    @Log(title = "企业班组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CommonCooperatorClass commonCooperatorClass) {
        return toAjax(commonCooperatorClassService.updateCommonCooperatorClass(commonCooperatorClass));
    }

    /**
     * 删除企业班组
     */
    @PreAuthorize("@ss.hasPermi('cooperator:class:remove')")
    @Log(title = "企业班组", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(commonCooperatorClassService.deleteCommonCooperatorClassByIds(ids));
    }
    /**
     * 根据项目id查询班组
     */
    @GetMapping("/selectClassByProjectId/{projectId}")
    public AjaxResult selectClassByProjectId(@PathVariable("projectId") Long projectId) {
        return success(commonCooperatorClassService.selectCommonCooperatorClassByProjectId(projectId));

    }
}
